//
//  TestTableViewDataSource.m
//  sandbox
//
//  Created by 杨岑 on 5/6/15.
//  Copyright (c) 2015 杨岑. All rights reserved.
//

#import "DocListDataSource.h"
#import "DocListNameCellView.h"
#import "DocListStatusCellView.h"
#import "DocListDataItem.h"


@implementation DocListDataSource
{
    NSDateFormatter* _dateFormatter;
    NSMutableArray* _itemList;
}

+ (instancetype)create
{
    return [[DocListDataSource alloc] init];
}

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        _dateFormatter = [[NSDateFormatter alloc] init];
        _dateFormatter.doesRelativeDateFormatting = YES;
        _dateFormatter.dateStyle = NSDateIntervalFormatterMediumStyle;
        _dateFormatter.timeStyle = NSDateIntervalFormatterMediumStyle;
        
        _itemList = [NSMutableArray array];
    }
    return self;
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
    return self.itemList.count;
}

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    if (row >= self.itemList.count) return nil;
    
    DocListDataItem* dataItem = self.itemList[row];
    
    NSTableCellView* cellView = [tableView makeViewWithIdentifier:tableColumn.identifier owner:tableView];
    if ([tableColumn.identifier isEqualToString:@"document"])
    {
        DocListNameCellView* nameView = (DocListNameCellView*)cellView;
        
        nameView.textField.stringValue = dataItem.name;
        if (dataItem.lastModification)
        {
            nameView.lastModificationLabel.stringValue = [_dateFormatter stringFromDate:dataItem.lastModification];
        }
        else
        {
            nameView.lastModificationLabel.stringValue = @"(unknown)";
        }
        nameView.conflictImageView.hidden = !dataItem.inConflict;
        nameView.imageView.image = [NSImage imageNamed:dataItem.isPrimary ? @"filelock2" : @"filelock"];
    }
    else if  ([tableColumn.identifier isEqualToString:@"status"])
    {
        DocListStatusCellView* statusView = (DocListStatusCellView*)cellView;
        
        statusView.textField.stringValue = [self localizedDocumentStatus:dataItem.status];
        if (dataItem.status == DocumentStatusCurrent)
        {
            statusView.progressBar.hidden = YES;
        }
        else
        {
            statusView.progressBar.hidden = NO;
            if (dataItem.percentDownloaded > 0)
            {
                statusView.progressBar.doubleValue = dataItem.percentDownloaded.doubleValue;
            }
        }
    }
    else
    {
        NSLog(@"%@: unknown column identifier: %@", self, tableColumn.identifier);
    }
    
    return cellView;
}

- (NSString*)localizedDocumentStatus:(DocumentStatus)status
{
    if (status == DocumentStatusCurrent)
    {
        return @"current";
    }
    else if (status == DocumentStatusNotRecent)
    {
        return @"not recent";
    }
    else if (status == DocumentStatusNotLocal)
    {
        return @"not downloaded";
    }
    return @"unknown";
}

- (void)tableViewSelectionDidChange:(NSNotification *)notification
{
    if ([self.delegate respondsToSelector:@selector(tableViewSelectionDidChange:)])
    {
        [self.delegate tableViewSelectionDidChange:notification];
    }
}

@end
